package com.dykj.fireFighting.web.mq;

import com.alibaba.fastjson.JSON;
import com.dykj.fireFighting.api.elasticsearch.devHisSs.entity.EsDevHisSs;
import com.dykj.fireFighting.api.elasticsearch.devHisSs.service.EsDevHisSsService;
import com.dykj.fireFighting.common.starter.redisson.service.RedisService;
import com.dykj.fireFighting.web.entity.DevHisSs;
import com.dykj.fireFighting.web.message.InsertDevHisSsMessage;
import com.dykj.fireFighting.web.service.IDevHisSsService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;

/**
 * @Author: zly
 * @Description : 盛善
 * @Date: 2021-11-30
 */
@Service
@RocketMQMessageListener(
        topic = InsertDevHisSsMessage.TOPIC,
        consumerGroup = "dy-consumer-group-" + InsertDevHisSsMessage.TOPIC
)
public class InsertDevHisSsConsummer implements RocketMQListener<InsertDevHisSsMessage> {
    public static final Logger LOGGER = LoggerFactory.getLogger(InsertDevHisSsConsummer.class);
   @Autowired
   private RedisService redisService;
   @Autowired
   private IDevHisSsService devHisSsService;

    @DubboReference
    EsDevHisSsService esDevHisSsService;
    @Override
    public void onMessage(InsertDevHisSsMessage insertDevHisSsMessage) {
        DevHisSs devHisSs = insertDevHisSsMessage.getDevHisSs();
        LOGGER.info("===========插入devHisSs数据到mysql==============");
        devHisSsService.insertDevHisSs(devHisSs);

        LOGGER.info("===========插入EsDevHis数据到ES==============");
        System.out.println(esDevHisSsService.toString());
        EsDevHisSs esDevHisSs = new EsDevHisSs();
        BeanUtils.copyProperties(devHisSs,esDevHisSs);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        esDevHisSs.setUpTime(sdf.format(devHisSs.getUpTime()));
        esDevHisSs.setZtfssj(sdf.format(devHisSs.getZtfssj()));
        esDevHisSs = esDevHisSsService.importAll(esDevHisSs);
        LOGGER.info("===========插入EsDevHis数据到ES:=============" + JSON.toJSONString(esDevHisSs));
        redisService.set(devHisSs.getId(), JSON.toJSONString(devHisSs), 3600L);
    }
}
